package org.example.user.manage.service.impl;

import org.example.commons.entity.SysHealthStatus;
import org.example.commons.entity.SysUser;
import org.example.commons.utils.UpdateUtil;
import org.example.user.manage.service.ISysUserService;
import org.example.user.manage.dto.SysHealthStatusDTO;
import org.example.user.manage.repository.SysHealthStatusRepository;
import org.example.user.manage.service.ISysHealthStatusService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 接口实现
 *
 * @author bothin
 */
@Service
public class SysHealthStatusServiceImpl implements ISysHealthStatusService {

    @Autowired
    private SysHealthStatusRepository healthStatusRepository;

    @Autowired
    private ISysUserService userService;

    @Override
    public SysHealthStatusDTO queryById(Long id) {
        return convert(healthStatusRepository.findById(id).orElse(null));
    }

    @Override
    public Page<SysHealthStatusDTO> queryAllPage(Pageable pageable) {
        return convert(healthStatusRepository.findAll(pageable),pageable);
    }

    @Override
    public SysHealthStatusDTO insert(SysHealthStatusDTO entity) {
        entity = updateInfo(entity);
        return convert(healthStatusRepository.save(convert(entity)));
    }

    @Override
    public SysHealthStatusDTO update(SysHealthStatusDTO entity) {
        entity = updateInfo(entity);
        return convert(healthStatusRepository.save(convert(entity)));
    }

    @Override
    public void deleteById(Long id) {
        healthStatusRepository.deleteById(id);
    }

    @Override
    public List<SysHealthStatusDTO> findByStatusIdIn(Long[] ids) {
        return healthStatusRepository.findByStatusIdIn(ids).stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public List<SysHealthStatusDTO> queryAll() {
        return healthStatusRepository.findAll().stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public SysHealthStatusDTO findByUserIdDTO(Long userId) {
        return convert(healthStatusRepository.findByUserId(userId));
    }

    @Override
    public SysHealthStatus findByUserId(Long userId) {
        return healthStatusRepository.findByUserId(userId);
    }

    @Override
    public SysHealthStatus save(SysHealthStatus entity) {
        return healthStatusRepository.save(entity);
    }

    @Override
    public Page<SysHealthStatusDTO> searchList(String name, Integer healthStatus, Date[] dates, Pageable pageable) {
        return convert(healthStatusRepository.searchList("%"+name+"%",healthStatus,dates,pageable),pageable);
    }

    @Override
    public List<SysHealthStatus> queryLastStatusBydays(Integer days) {
        return healthStatusRepository.findLastSysHealthStatus(days);
    }

    private SysHealthStatusDTO convert(SysHealthStatus healthStatus){
        if (Objects.isNull(healthStatus)) return null;
        SysHealthStatusDTO sysHealthStatusDTO = new SysHealthStatusDTO();
        BeanUtils.copyProperties(healthStatus,sysHealthStatusDTO);
        if (healthStatus.getUserId() != null) {
            SysUser sysUser = userService.queryById(healthStatus.getUserId());
            sysHealthStatusDTO.setSysUser(sysUser);
        }
        return sysHealthStatusDTO;
    }

    private SysHealthStatus convert(SysHealthStatusDTO healthStatusDTO){
        if (Objects.isNull(healthStatusDTO)) return null;
        SysHealthStatus sysHealthStatus = new SysHealthStatus();
        BeanUtils.copyProperties(healthStatusDTO,sysHealthStatus);
        return sysHealthStatus;
    }

    private Page<SysHealthStatusDTO> convert(Page<SysHealthStatus> healthStatusPage, Pageable pageable) {
        List<SysHealthStatusDTO> healthStatusDTOList = healthStatusPage.getContent().stream()
                .map(this::convert).collect(Collectors.toList());
        return new PageImpl<>(healthStatusDTOList, pageable, healthStatusPage.getTotalElements());
    }

    private SysHealthStatusDTO updateInfo(SysHealthStatusDTO statusDTO){
        if (statusDTO == null) return null;
        if (statusDTO.getStatusId()==null) {
            return statusDTO;
        }else {
            SysHealthStatusDTO sysHealthStatusDTO = queryById(statusDTO.getStatusId());
            if (sysHealthStatusDTO==null) return null;
            UpdateUtil.copyNullProperties(statusDTO,sysHealthStatusDTO);
            return sysHealthStatusDTO;
        }
    }

}
